package com.mxx.common.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;

/**
 * @Author itzgyw
 * @Date 2020-01-08
 * @Time 18:29
 * @Description:
 */
public class Pairs<K, V> {

    private List<Entry<K, V>> metadata;


    private Pairs(List<Entry<K, V>> metadata) {
        this.metadata = metadata;
    }

    public static <K, V> Pairs build(List<Entry<K, V>> pairList) {
        return new Pairs(pairList);
    }

    public static <K, V> Pairs build() {
        return new Pairs(new ArrayList<>());
    }

    public static <K, V> Pairs build(K key, V val) {
        return new Pairs(new ArrayList<>()).put(key, val);
    }

    public Pairs put(K key, V val) {
        this.metadata.add(new Entry(key, val));
        return this;
    }

    public Pairs putAll(Map<K, V> map) {
        for (Map.Entry entry : map.entrySet()) {
            this.metadata.add(new Entry(entry.getKey(), entry.getValue()));
        }
        return this;
    }

    public <K, V> Pairs putAll(Map<K, V> map, BiPredicate<K, V> filter) {
        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (filter.test(entry.getKey(), entry.getValue())) {
                this.metadata.add(new Entry(String.valueOf(entry.getKey()), entry.getValue()));
            }
        }
        return this;
    }

    public Pairs clear() {
        this.metadata.clear();
        return this;
    }

    public boolean isEmpty() {
        return this.metadata.isEmpty();
    }

    public List<Entry<K, V>> out() {
        return this.metadata;
    }
}
